// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'


// 引入element ui组件
import ElementUI from 'element-ui';
// import { MessageBox } from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);

// 引入element ui组件


// 全局引入axios
import axios from 'axios'
Vue.prototype.$axios = axios;


// 路由判断是否要登录
router.beforeEach((to, from, next) => {
	if (to.matched.some(record => record.meta.requireAuth)){  // 判断该路由是否需要登录权限
		if(!sessionStorage.getItem('token') && !localStorage.getItem('token')){
			next({
				path: '/login',
				query: {redirect: to.fullPath}  // 将跳转的路由path作为参数，登录成功后跳转到该路由
			})
		}else{
			 next();
		}
	}else {
		next();
	}
});

// 接口前缀
Vue.prototype.API = 'MK'

// axios 请求
axios.interceptors.request.use(function  (response){
	if (1 == 1) {
		response.headers.Platform = '123'
		return response;
	}
});

// axios 全局拦截
axios.interceptors.response.use(function  (response){
	if (response.data.returnCode == 401) {
			console.log('登录时效，请重新登录')  
			setTimeout(() => {
					router.replace({name:'login'})
			}, 2000);

		return response;
	}
	if (response.data.returnCode == 200) {
		return response;
	}
	return response;
});






//数字格式化过滤器
Vue.filter('formatNumber', function (date) {
		var date = new Date(date * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
	var Y = date.getFullYear() + '-';
	var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
	var D = date.getDate() + ' ';
	var h = date.getHours() + ':';
	var m = date.getMinutes() + ':';
	var s = date.getSeconds();
	return Y+M+D+h+m+s;
})



Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
	el: '#app',
	router,
	axios, //通过import引入，然后在这里调用
	render: h => h(App),
	components: { App },
	template: '<App/>'
})



